/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import com.google.api.adwords.starterkit.db.structures.CriterionStructure;
import com.google.api.adwords.v201101.cm.Keyword;

/**
 * Unclassifed usefull methods
 * 
 * @author rodrigo.f.vale@gmail.com (Rodrigo Vale)
 */
public class Utils {
  private static final Logger log = Logger.getLogger("StarterKit");
  
  /**
   * Sleeps the application for a while
   *
   * @param mili the milliseconds to sleep
   */
  public static void sleep(int mili) {
    try {
      log.debug("Sleeping for " + mili + " miliseconds.");
      Thread.sleep(mili);
    } catch (InterruptedException e) {
      DumpExceptions.dumpInterruptedException(e);
    }    
  }

  /**
   * Split a set of generic operations into a small set of operations
   * 
   * @param operations
   *          the generic set operations
   * @param size
   *          the new set of operations size
   * @return a list of sub-sets
   */
  public static <T extends Object> List<HashSet<T>> split(HashSet<T> operations,
      int size) {
    List<HashSet<T>> blocks = new ArrayList<HashSet<T>>();
    HashSet<T> operationsBlock = new HashSet<T>();
    blocks.add(operationsBlock);
    int count = 0;
    Iterator<T> i = operations.iterator();
    while (i.hasNext()) {
      if (count == size) {
        count = 0;
        operationsBlock = new HashSet<T>();
        blocks.add(operationsBlock);
      }
      operationsBlock.add(i.next());
      count++;
    }
    return blocks;
  }

  /**
   * Creates the AdWord keyword object.
   * 
   * @param critStruct
   *          the keyword structure
   * @return the keyword object
   */
  public static Keyword createKeyword(CriterionStructure critStruct) {
    Keyword keyword = new Keyword();
    if (critStruct.getKeywordId() != 0) {
      keyword.setId(critStruct.getKeywordId());
    }
    keyword.setMatchType(critStruct.getMatchType());
    keyword.setText(critStruct.getText());
    return keyword;
  }
  
  public static String toString(String array[]) {
    String answer = "";
    for (int i = 0; i < array.length; i++) {
      answer = answer + array[i];
      if ((i + 1) < array.length) {
        answer = answer + ", ";
      }
    }
    return answer;
  }
  
  public static String cut(String s, int size) {
    if (s.length() < size) {
      return s;
    } else {
      return s.substring(1,size);
    }
  }
}
